﻿using System;
using System.Collections.Generic;
using System.Text;

namespace HaoCoding.DCdb.WaterfallTree
{
    /// <summary>
    /// 数据定位
    /// </summary>
    public class Locator : IDescriptor, IComparable<Locator>, IEquatable<Locator>
    {
        private byte[] serializationData;

        /// <summary>
        /// ID的哈希值
        /// </summary>
        private int hashCode;

        /// <summary>
        /// 是否删除
        /// </summary>
        private bool isDeleted;

        private string name;

        /// <summary>
        /// 同步锁
        /// </summary>
        public readonly object SyncRoot = new object();

        /// <summary>
        /// 对象比较器
        /// </summary>
        /// <param name="other">另一个对象</param>
        /// <returns>一样返回0，大于other返回1，小于other返回-1</returns>
        public int CompareTo(Locator other)
        {
            return this.ID.CompareTo(other.ID);
        }

        /// <summary>
        /// 是否等于当前实例
        /// </summary>
        /// <param name="other"></param>
        /// <returns></returns>
        public bool Equals(Locator other)
        {
            return this.ID == other.ID;
        }

        /// <summary>
        /// 判断对象是否等于当前实例
        /// </summary>
        /// <param name="obj">对象</param>
        /// <returns></returns>
        public override bool Equals(object obj)
        {
            if (!(obj is Locator))
                return false;

            return Equals((Locator)obj);
        }

        /// <summary>
        /// 获取哈希值
        /// </summary>
        /// <returns></returns>
        public override int GetHashCode()
        {
            return hashCode;
        }

        /// <summary>
        /// 转为字符串
        /// </summary>
        /// <returns>当前实例的Name值</returns>
        public override string ToString()
        {
            return Name;
        }

        /// <summary>
        /// 是否删除
        /// </summary>
        public bool IsDeleted
        {
            get
            {
                lock (SyncRoot)
                    return isDeleted;
            }

            set
            {
                lock (SyncRoot)
                {
                    if (value != isDeleted)
                    {
                        isDeleted = value;
                        serializationData = null;
                    }
                }
            }
        }

        #region IDescription

        /// <summary>
        /// 标识
        /// </summary>
        public long ID { get; set; }

        public string Name
        {
            get { return name; }
            set
            {
                lock (SyncRoot)
                {
                    name = value;
                    serializationData = null;
                }
            }
        }

        #endregion

    }
}
